package com.ruoyi.mes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.mes.domain.PlnReleasedPlan;
import com.ruoyi.mes.service.IPlnReleasedPlanService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.domain.PlnMasterPlan;
import com.ruoyi.mes.service.IPlnMasterPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 生产计划Controller
 * 
 * @author CFL
 * @date 2024-04-28
 */
@RestController
@RequestMapping("/mes/plan")
public class PlnMasterPlanController extends BaseController
{
    @Autowired
    private IPlnMasterPlanService plnMasterPlanService;

    /**
     * 查询生产计划列表
     */
    @PreAuthorize("@ss.hasPermi('mes:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(PlnMasterPlan plnMasterPlan)
    {
        startPage();
        List<PlnMasterPlan> list = plnMasterPlanService.selectPlnMasterPlanList(plnMasterPlan);
        return getDataTable(list);
    }

    /**
     * 查询下达计划列表
     */
    @PreAuthorize("@ss.hasPermi('mes:plan:released:query')")
    @GetMapping("/released/{orderId}")
    public List<PlnReleasedPlan> getTableInfo(@PathVariable("orderId") Long orderId)
    {
       return plnMasterPlanService.selectPlnReleasedPlanListByOrderId(orderId);
    }

    /**
     * 导出生产计划列表
     */
    @PreAuthorize("@ss.hasPermi('mes:plan:export')")
    @Log(title = "生产计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlnMasterPlan plnMasterPlan)
    {
        List<PlnMasterPlan> list = plnMasterPlanService.selectPlnMasterPlanList(plnMasterPlan);
        ExcelUtil<PlnMasterPlan> util = new ExcelUtil<PlnMasterPlan>(PlnMasterPlan.class);
        util.exportExcel(response, list, "生产计划数据");
    }

    /**
     * 根据订单id获取生产计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:plan:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getOrderInfo(@PathVariable("orderId") Long orderId)
    {
        List<PlnMasterPlan> plnMasterPlanList = plnMasterPlanService.selectPlnMasterPlanById(orderId);
        return AjaxResult.success(plnMasterPlanList);
    }

    /**
     * 新增生产计划
     */
    @PreAuthorize("@ss.hasPermi('mes:plan:add')")
    @Log(title = "生产计划", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PlnMasterPlan plnMasterPlan)
    {
        return toAjax(plnMasterPlanService.insertPlnMasterPlan(plnMasterPlan));
    }

    /**
     * 修改生产计划
     */
    @PreAuthorize("@ss.hasPermi('mes:plan:edit')")
    @Log(title = "生产计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody List<PlnMasterPlan> plnMasterPlan)
    {
        return toAjax(plnMasterPlanService.updatePlnMasterPlan(plnMasterPlan));
    }

    /**
     * 新增下达计划
     */
    @PreAuthorize("@ss.hasPermi('mes:plan:released:add')")
    @Log(title = "下达计划", businessType = BusinessType.INSERT)
    @PostMapping("/released/add")
    public AjaxResult addReleasdPlan(@RequestBody PlnMasterPlan plnMasterPlan)
    {
        return plnMasterPlanService.addPlnMasterPlan(plnMasterPlan);
    }

    /**
     * 撤回下达计划
     */
    @PreAuthorize("@ss.hasPermi('mes:plan:released:sub')")
    @Log(title = "下达计划", businessType = BusinessType.UPDATE)
    @PutMapping("/released/sub")
    public AjaxResult subReleasdPlan(@RequestBody Long id)
    {
        return plnMasterPlanService.subPlnMasterPlan(id);
    }

    /**
     * 删除生产计划
     */
    @PreAuthorize("@ss.hasPermi('mes:plan:remove')")
    @Log(title = "生产计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(plnMasterPlanService.deletePlnMasterPlanByIds(ids));
    }
}
